package com.software.backend.login.controller;

import com.google.gson.Gson;
import com.software.backend.login.entity.UserExtends;
import com.software.backend.login.service.LoginService;
import com.software.backend.login.utils.Password;
import com.software.backend.login.utils.PhoneNumber;
import com.software.backend.login.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author 王俊坤
 * @version 1.0
 * @date 2022/11/10 16:23
 */

@RestController
@RequestMapping("/login")
public class LoginController {


    @Autowired
    LoginService loginService;

    private Gson gson = new Gson();

    /**
     * 通过电话号码和密码登陆
     */
    @PostMapping("/loginByPhoneNumberAndPassword")
    public Result loginByPhoneNumberAndPassword(@RequestParam String phoneNumber, @RequestParam String password, HttpSession session) {
        if (session.getAttribute("UserExtendsInfo") != null) {
            Result.getErrorResult(500, "您还未退出登陆，请进行合法操作", null);
        } else if (!PhoneNumber.isPhoneNumber(phoneNumber)) {
            return Result.getErrorResult(500, "请输入合法手机号", null);
        } else if (!Password.isValid(password)) {
            return Result.getErrorResult(500, "请输入合法密码", null);
        }
        UserExtends loginResult = loginService.loginByPhoneNumberAndPassword(phoneNumber, password);
        if (loginResult != null) {
            if (loginResult.getId() != null) {
                session.setAttribute("UserExtendsInfo", loginResult);
                return Result.getSuccessResult(loginResult);
            } else {
                return Result.getErrorResult(500, "密码错误或还未绑定密码", null);
            }
        }else{
            return Result.getErrorResult(500, "用户名不存在", null);
        }
    }

    /**
     * 根据电话号获取验证码
     * 同时判断该电话号是否已经注册
     */
    @PostMapping("/loginOrRegisterByPhoneNumberForVarification")
    public Result loginOrRegisterByPhoneNumberForVarification(@RequestParam String phoneNumber,HttpSession session) {
        if (session.getAttribute("UserExtendsInfo") != null) {
            Result.getErrorResult(500, "您还未退出登陆，请进行合法操作", null);
        } else if (!PhoneNumber.isPhoneNumber(phoneNumber)) {
            return Result.getErrorResult(500, "请输入合法手机号", null);
        }
        //调用loginService中的方法生成验证码，并用短信形式发送
        boolean hastimeOut = loginService.createAndSendAuthCode(phoneNumber, session);
        if (!hastimeOut){
            //判断手机号是否已经注册
            String phoneRegisterPrompt = loginService.isPhoneRegister(phoneNumber);
            //根据是否注册的结果，返回对应提示符
            return Result.getSuccessResult(200, phoneRegisterPrompt, null);
        }else {
            return Result.getErrorResult(500, "验证码已发送，请勿频繁操作", null);
        }
    }


    /**
     * 重新发送验证码
     */
    @PostMapping("/loginOrRegisterReSendAuthCode")
    public Result loginOrRegisterReSendAuthCode(@RequestParam String phoneNumber,HttpSession session) {
        if (session.getAttribute("UserExtendsInfo") != null) {
            Result.getErrorResult(500, "您还未退出登陆，请进行合法操作", null);
        } else if (!PhoneNumber.isPhoneNumber(phoneNumber)) {
            return Result.getErrorResult(500, "请输入合法手机号", null);
        }
        //调用loginService中的方法生成验证码，并用短信形式发送
        boolean hastimeOut = loginService.createAndSendAuthCode(phoneNumber, session);
        if (hastimeOut) {
            return Result.getErrorResult(500, "验证码已发送，请勿频繁操作", null);
        }else {
            return Result.getSuccessResult(null);
        }
    }

    /**
     * 验证验证码
     */
    @PostMapping("/loginOrRegisterVarifyAuthCode")
    public Result loginOrRegisterVarifyAuthCode(@RequestParam String phoneNumber, @RequestParam String authCode, HttpSession session) {
        if (session.getAttribute("UserExtendsInfo") != null) {
            Result.getErrorResult(500, "您还未退出登陆，请进行合法操作", null);
        } else if (!PhoneNumber.isPhoneNumber(phoneNumber)) {
            return Result.getErrorResult(500, "请输入合法手机号", null);
        }else if (authCode == null || authCode.length() != 6){
            return Result.getErrorResult(500, "验证码错误，请重新输入", null);
        }
        //判断验证码是否正确
        boolean varifyResult = loginService.varifyAuthCode(phoneNumber, authCode, session);
        //若正确，判断识别码，注册并登录
        if (varifyResult){
            UserExtends userExtends = loginService.loginOrRegister(phoneNumber, session);
            ArrayList<UserExtends> userExtendsArrayList = new ArrayList<>();
            userExtendsArrayList.add(userExtends);
            return Result.getSuccessResult(200, "登陆成功", userExtendsArrayList);
        }else {
            return Result.getErrorResult(500, "验证码错误，请重新输入", null);
        }

    }
}
